package com.danhar.forward.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
import com.alibaba.xxpt.gateway.shared.client.http.GetClient;
import com.alibaba.xxpt.gateway.shared.client.http.PostClient;
import com.danhar.forward.service.DingdingGovernmentClientService;
import com.danhar.forward.service.bean.DingDingBaseRsp;
import com.danhar.forward.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author hu
 * @version 1.0
 * @date 2020/6/29/029
 */
@Service
public class DingdingGovernmentClientServiceImpl implements DingdingGovernmentClientService {

    //appKey
    @Value("${app.app_key}")
    private String appKey;

    //appSecret
    @Value("${app.app_secret}")
    private String appSecret;

    /**
     * 环境
     */
    @Value("${app.domain_name}")
    private String domainName;


    @Autowired
    private RedisUtil  redisUtil;

    /**
     * accessToken缓存时常 7100秒
     */
    private Integer accessTokenCacheTime = 7100 ; //

    @Override
    public String getAccessToken() {
          //可能出现的异常, client异常 , redis异常, 返回值异常
        String  accessToken =""  ;
         //在redis 里面查找 token  有的话直接返回 没有的话就 去调用 获取token的方法\
        String  accessTokenKey = appSecret+ domainName;
        Object accessTokenKeyObj = redisUtil.get(accessTokenKey);
        if(accessTokenKeyObj!=null){
            accessToken  = accessTokenKeyObj.toString();
           return     accessToken;
        }
        ExecutableClient executableClient =ExecutableClient.getInstance();
        executableClient.setAccessKey(appKey);
        executableClient.setSecretKey(appSecret);
        executableClient.setDomainName(domainName);
        executableClient.setProtocal("https");
        executableClient.init();
        String api = "/gettoken.json";
        GetClient getClient = executableClient.newGetClient(api);
        getClient.addParameter("appkey", appKey);
        getClient.addParameter("appsecret", appSecret);
        String apiResult = getClient.get();



        //如果获取到了就把access token  放到Redis里面
        JSONObject rtJsonStr = JSON.parseObject(apiResult);
        String success = rtJsonStr.getString("success");
        if("true".equals(success)){
            //成功了,保存到redis 里面去
            String contentObj = rtJsonStr.getString("content");
            JSONObject contentJsonObj = JSON.parseObject(contentObj);
            String responseCode = contentJsonObj.getString("responseCode");
            if("0".equals(responseCode)){
                String dataStr = contentJsonObj.getString("data");
                JSONObject dataStrJsonObj = JSON.parseObject(dataStr);
                String rtAccessToken = dataStrJsonObj.getString("accessToken");
                accessToken = rtAccessToken;

                redisUtil.set(accessTokenKey,accessToken,7100);
            }
        }
         return accessToken ;
    }


    @Override
    public PostClient getPostClient(String api) {
        ExecutableClient executableClient = ExecutableClient.getInstance();
        executableClient.setAccessKey(appKey);
        executableClient.setSecretKey(appSecret);
        executableClient.setDomainName(domainName);
        executableClient.setProtocal("https");
        executableClient.init();
        return executableClient.newPostClient(api);
    }
}
